package cn.bb.service;

import cn.bb.dao.IArticleDao;
import cn.bb.pojo.Article;
import cn.bb.pojo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("articleService")
public class IArticleServiceImpl implements IArticleService {
    @Autowired
    private IArticleDao articleDao;
    @Override
    public int insertArticle(Article article) {
        return articleDao.addArticle(article);
    }

    @Override
    public Page getAllArticle(Map map) {
        Page page = new Page();
        List<Object> list = articleDao.getArticleList(map);//获得的记录数
        int totalNum = articleDao.getArticleCount();
        //System.out.println("总记录数为："+totalNum);
        System.out.println(list);
        int currentPage = Integer.parseInt(String.valueOf(map.get("currentPage")));//当前页数
        page.setCurPageNum(currentPage);//当前页数
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);//每页的个数
        page.setResult(list);//获得的结果集
        page.setTotalNum(totalNum);//获得的总记录数
        page.setTotalPage(page.getTotalNum()/pageSize+(page.getTotalNum()%pageSize == 0?0:1));//总页数
        return page;
    }

    public IArticleDao getArticleDao() {
        return articleDao;
    }

    public void setArticleDao(IArticleDao articleDao) {
        this.articleDao = articleDao;
    }
}
